import pandas as pd
from emtencent import config as cn
import json
from urllib.request import urlopen, Request
import sys
import inspect

def _req(str, page=1,get_all=True, **kwargs):
    if 'page' not in kwargs.keys():
        kwargs['page'] = page
    req = Request(str.format(**kwargs))
    text = urlopen(req, timeout=10).read()
    text = text.decode('GBK')
    r = _handle_text(text)
    if r:
        datas = r[0]
        total_page = r[1]
        date = r[2]
        total_count = r[3]
        if get_all and page < total_page:
            for i in range(page+1,total_page+1):
                kwargs.pop('page')
                (_datas, _date, _total_count) = _req(str=str, page=i, **kwargs)
                datas.extend(_datas)
                return (datas, date, total_count)
        return (datas,date,total_count)
    return ([], None, 0)

def _common_format(datas=None):
    if datas:
        df = pd.DataFrame(datas).T
        return df
    return None


# hs_hsl=0.05&hs_zf=0.03&hs_lb=1
def get_active_code(page=1,hsl=0.05, zf=0.03, lb=1, get_all=True):
    datas, date, total_count = _req(cn.SMART_ACTIVE_CODE, **vars())
    return _common_format(datas=datas)

# hs_zlzc5=0.05&hs_zf3=,0.15&hs_zllb1=1
def get_money_code(page=1, zlzc5=0.05, zf3=0.15, zllb1=1):
    datas, date, total_count = _req(cn.SMART_MONEY_CODE, **vars())
    return _common_format(datas=datas)


# type=hs_yb5
def get_research_code(page=1, yb=5):
    # 研报时间只能是1 5 30
    if yb not in [1, 5, 30]:
        yb = 5
    datas, date, total_count = _req(cn.SMART_RESEARCH_CODE, **vars())
    return _common_format(datas=datas)

# hs_lhb=0.1
def get_daredie_code(lhb=0.1):
    datas, date, total_count = _req(cn.SMART_DARE_DIE_CODE, **vars())
    return _common_format(datas=datas)

# SMART_ZB_SGCX = 'http://smartstock.gtimg.cn/get.php?_func=zhibiao&_default=1&_page=1&_pagesize=30&zhibiao=hs_sgcx&_du_r_t=0.8666050736378486'
def get_zb_sgcx(page=1):
    datas, date, total_count = _req(cn.SMART_ZB_SGCX, **vars())
    return _common_format(datas=datas)

# SMART_ZB_KZJY = 'http://smartstock.gtimg.cn/get.php?_func=zhibiao&_default=1&_page=1&_pagesize=30&zhibiao=hs_kzjy&_du_r_t=0.2342141408850362'
def get_zb_kzjy(page=1):
    datas, date, total_count = _req(cn.SMART_ZB_KZJY, **vars())
    return _common_format(datas=datas)


# SMART_ZB_CJDX = 'http://smartstock.gtimg.cn/get.php?_func=zhibiao&_default=1&_page=1&_pagesize=30&zhibiao=hs_cjdx&_du_r_t=0.4129151914416227'
def get_zb_cjdx(page=1):
    datas, date, total_count = _req(cn.SMART_ZB_CJDX, **vars())
    return _common_format(datas=datas)


def get_zb_xsdf(page=1,zf60=0.6):
    if zf60 not in [0.15, 0.3, 0.6]:
        zf60 = 0.6
    datas, date, total_count = _req(cn.SMART_ZB_XSDF, **vars())
    return _common_format(datas=datas)


def get_zb_hdly(page=1, gl10=-0.1):
    if gl10 not in [-0.02, -0.05, -0.07, -0.1]:
        gl10=-0.1
    datas, date, total_count = _req(cn.SMART_ZB_HDLY, **vars())
    return _common_format(datas=datas)

def _handle_text(text):
    text = text.replace('var _smartstock=', '')
    js = json.loads(text)
    if js['data'] and js['date'] and js['pagesize'] and js['total']:
        total_page = js['total']//js['pagesize'] + 1
        return (js['data'], total_page, js['date'], js['total'])
    return None



# get_active_code()

# get_money_code()

# get_research_code(yb=30)

# get_daredie_code()

# get_zb_sgcx()

# get_zb_kzjy()

# get_zb_cjdx()

# get_zb_xsdf()

get_zb_hdly()